package com.xuelang.pipeline.artifact.infrastructure.resource.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xuelang.pipeline.artifact.client.common.result.PageRecord;
import com.xuelang.pipeline.artifact.client.resource.dto.ArtifactAttributeQuery;
import com.xuelang.pipeline.artifact.domain.resource.ArtifactAttribute;
import com.xuelang.pipeline.artifact.domain.resource.repository.ArtifactAttributeRepository;
import com.xuelang.pipeline.artifact.infrastructure.resource.converter.ArtifactAttributeToArtifactAttributeDOConverter;
import com.xuelang.pipeline.artifact.infrastructure.resource.dataobject.ArtifactAttributeDO;
import com.xuelang.pipeline.artifact.infrastructure.resource.mapper.ArtifactAttributeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 制品仓库属性实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@Repository
public class ArtifactAttributeRepositoryImpl implements ArtifactAttributeRepository {

    @Autowired
    ArtifactAttributeMapper artifactAttributeMapper;

    @Override
    public PageRecord<ArtifactAttribute> pageList(long currentPage, long pageSize, ArtifactAttributeQuery artifactAttributeQuery) {
        currentPage = currentPage < 1 ? 1 : currentPage;
        pageSize = pageSize < 1 ? 10 : pageSize;
        Page<ArtifactAttribute> page = new Page<>(currentPage, pageSize);
        MPJLambdaWrapper<ArtifactAttributeDO> wrapper = new MPJLambdaWrapper<ArtifactAttributeDO>();
        wrapper.selectAll(ArtifactAttributeDO.class);
        wrapper.and(StrUtil.isNotBlank(artifactAttributeQuery.getCondition()), wp -> wp
                        .like(ArtifactAttributeDO::getUuid, artifactAttributeQuery.getCondition()).or()
                        .like(ArtifactAttributeDO::getInstanceRepositoryName, artifactAttributeQuery.getCondition()).or()
                        .like(ArtifactAttributeDO::getWebhookType, artifactAttributeQuery.getCondition()).or()
                        .like(ArtifactAttributeDO::getWebhookId, artifactAttributeQuery.getCondition()))
                .apply(StrUtil.isNotBlank(artifactAttributeQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')>={0}", artifactAttributeQuery.getStartDate())
                .apply(StrUtil.isNotBlank(artifactAttributeQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')<={0}", artifactAttributeQuery.getEndDate())
                .orderByDesc(ArtifactAttributeDO::getCreateTime);
        artifactAttributeMapper.selectJoinPage(page, ArtifactAttribute.class, wrapper);
        return new PageRecord<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.getRecords()
        );
    }

    @Override
    public List<ArtifactAttribute> list(ArtifactAttributeQuery artifactAttributeQuery) {
        MPJLambdaWrapper<ArtifactAttributeDO> wrapper = new MPJLambdaWrapper<ArtifactAttributeDO>();
        wrapper.selectAll(ArtifactAttributeDO.class);
        wrapper.and(StrUtil.isNotBlank(artifactAttributeQuery.getCondition()), wp -> wp
                        .like(ArtifactAttributeDO::getUuid, artifactAttributeQuery.getCondition()).or()
                        .like(ArtifactAttributeDO::getInstanceRepositoryName, artifactAttributeQuery.getCondition()).or()
                        .like(ArtifactAttributeDO::getWebhookType, artifactAttributeQuery.getCondition()).or()
                        .like(ArtifactAttributeDO::getWebhookId, artifactAttributeQuery.getCondition()))
                .apply(StrUtil.isNotBlank(artifactAttributeQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')>={0}", artifactAttributeQuery.getStartDate())
                .apply(StrUtil.isNotBlank(artifactAttributeQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')<={0}", artifactAttributeQuery.getEndDate())
                .orderByDesc(ArtifactAttributeDO::getCreateTime);
        return artifactAttributeMapper.selectJoinList(ArtifactAttribute.class, wrapper);
    }


    @Override
    public Boolean save(ArtifactAttribute artifactAttribute) {
        return artifactAttributeMapper.insert(ArtifactAttributeToArtifactAttributeDOConverter.INSTANCE.artifactAttributeToArtifactAttributeDO(artifactAttribute)) == 1;
    }

    @Override
    public Boolean update(ArtifactAttribute artifactAttribute) {
        return artifactAttributeMapper.update(
                ArtifactAttributeToArtifactAttributeDOConverter.INSTANCE.artifactAttributeToArtifactAttributeDO(artifactAttribute),
                Wrappers.lambdaQuery(ArtifactAttributeDO.class).eq(ArtifactAttributeDO::getUuid, artifactAttribute.getUuid())) == 1;
    }

    @Override
    public Boolean delete(String artifactAttributeUuid) {
        return artifactAttributeMapper.delete(Wrappers.lambdaQuery(ArtifactAttributeDO.class)
                .eq(ArtifactAttributeDO::getUuid, artifactAttributeUuid)) == 1;
    }

    @Override
    public ArtifactAttribute query(String artifactAttributeUuid) {
        ArtifactAttributeDO artifactAttributeDO = artifactAttributeMapper.selectOne(Wrappers.lambdaQuery(ArtifactAttributeDO.class).eq(ArtifactAttributeDO::getUuid, artifactAttributeUuid));
        return ArtifactAttributeToArtifactAttributeDOConverter.INSTANCE.artifactAttributeDOToArtifactAttribute(artifactAttributeDO);
    }

    @Override
    public void updateWebhookInfo(String artifactUuid, String webhookType, String webhookId) {
        //artifactAttributeMapper.updateWebhookInfo(artifactUuid, webhookType, webhookId);
        artifactAttributeMapper.update(
                ArtifactAttributeDO.builder().webhookId(webhookId).webhookType(webhookId).build(),
                Wrappers.lambdaQuery(ArtifactAttributeDO.class).eq(ArtifactAttributeDO::getUuid, artifactUuid)
        );
    }

    @Override
    public void updateWebhookInfoToNull(String artifactUuid) {
        //artifactAttributeMapper.updateWebhookInfo(artifactUuid, "", "");
        artifactAttributeMapper.update(
                ArtifactAttributeDO.builder().webhookId("").webhookType("").build(),
                Wrappers.lambdaQuery(ArtifactAttributeDO.class).eq(ArtifactAttributeDO::getUuid, artifactUuid)
        );
    }

}
